<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/8
 * Time: 17:29
 */
namespace  zhtfrontend\components\services\commission;


use yii\base\Component;
use zhtfrontend\models\datas\Orders;
use common\models\datas\CardAttr;
use zhtfrontend\models\datas\OrdersCommission;
use common\components\helpers\Fun;



class Commission extends Component{

    /**
     * 2017-9-7
     * 1、现金支付时调用
     * 2、现金支付后，往orders_commission表写数据
     */
    public static function Insertcommsion($orders_id,$type)
    {
        $orders = Orders::findOne([$orders_id]);
        $card_attr = CardAttr::findOne([$orders->customer_card]);
        $model = new OrdersCommission();
        $model->orders_id = $orders_id;//订单id
        $model->orders_time = $orders->created_at;
        $model->salesman_id = $card_attr->salesman_id;//发卡人id
        $model->settlement_amount = $orders->cash_money;//需结算金额
        $model->type = $type;//订单时间
        //佣金比例
        $proportion = Commission::proportion($type);
        $model->commission_money = $orders->account_money * $proportion;//佣金
        $model->proportion = $proportion;
        return $model->save(false);
    }

    /**
     * 2017-9-8
     * 计算佣金比例
     * 1、根据不同角色、充值次数计算佣金比例
     */
    public static function Proportion($type){
        $site_id = Fun::getSiteId();
        //计算佣金
        //计算充值次数，第一次充值按消费金额5%计算，第二次以后按消费金额的1%计算佣金
        //后期做成后台可调整角色、比例的方法
//        $recharge = Recharge::find()->where(['customer_ic' => $orders->customer_id, 'site_id' => $site_id])->andWhere(['!=', 'surplus_cash', 0])->all();
//        foreach ($recharge as $k => $v) {
//
//        }
        $proportion = 0.05;
        switch ($type){
            case 0:
                $proportion = 0.05;
                break;
            case 1:
                $proportion = 0.05;
                break;
            case 2:
                $proportion = 0.05;
                break;
        }
        return $proportion;
    }
}
